package com.sheng.whyclass.work.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheng.whyclass.R;
import com.sheng.whyclass.work.acitivity.AnswerDetailActivity;
import com.sheng.whyclass.work.model.ContentData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/4.
 */
public class AnswerAdapter extends BaseAdapter{

    private ArrayList<ContentData.DataBean> dataList;
    private Context mContext;

    public AnswerAdapter(Context mContext) {
        this.mContext = mContext;
        this.dataList = new ArrayList<>();
    }

    public void setData(List<ContentData.DataBean> list) {
        if (list == null) {
            return;
        }
        this.dataList = new ArrayList<>();
        this.dataList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        final ContentData.DataBean itemData = dataList.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.content_item, null);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.contentLayout = (LinearLayout) convertView.findViewById(R.id.contentLayout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.time.setText(itemData.getIssue_time());
        viewHolder.title.setText(itemData.getTopic());
        viewHolder.contentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnswerDetailActivity.startAnswerDetailActivity(mContext,itemData.getTopic(),itemData.getIssue_time(),itemData.getW_c_id());

            }
        });
        return convertView;
    }

    final static class ViewHolder {
        TextView title;
        TextView time;
        LinearLayout contentLayout;
    }
}
